var TimeSlider = function (elementId, options, dataSetting) {
    options = options || {};
    this.canvas = document.getElementById(elementId);
    this.ctx = this.canvas.getContext("2d");
    this.canvansW = this.canvas.width || document.getElementById(elementId).style.width || 500;
    this.canvansH = this.canvas.height; //时间轴高度

    this.timeLineArr = []; //时间块的时间线
    this.timeLineBeginPos = [];
    
    this.minutes_per_step = [1, 2, 5, 10, 15, 20, 30, 60, 120, 180, 240, 360, 720, 1440]; // min/格
    this.graduation_step = 20; //刻度间最小宽度，单位px
    this.hours_per_ruler = 24; //时间轴显示24小时
    this.startTimestamp = new Date().getTime() - 12 * 60 * 60 * 1000;
    this.distance_between_gtitle = 40;
    this.zoom = 24;
    this.selectTimeLine = {}; //选中子时间组的时间轴
    this.g_isMousedown = false; //拖动mousedown标记
    this.g_isMousemove = false; //拖动mousemove标记
    this.g_mousedownCursor = null; //拖动mousedown的位置
    this.returnTime = null; //mouseup返回时间
    this.hoverTime = null;
    this.on_before_click_ruler_callback = null;
    this._options = {
        //时间轴样式
        timecell: {},
        cellHeight: 18,//时间轴块高度
        lineH: { big: 25, middle: 15, def: 10 }, //刻度线的高度
        backgroundColor: "rgba(255, 255, 255, 1)", //背景颜色
        dialColor: "rgba(151,158,167,1)",
        dialtextColor: "rgba(151,158,167,1)",
        middleTextColor: "rgba(151,158,167,1)", //中心显示刻度文本颜色；
        middleLine: { color: "rgb(64, 196, 255)", width: 4, height: this.canvansH - 10 }, //中心线样式
        hoverLine: { color: "rgb(194, 202, 215)", width: 1, textColor: "rgb(194, 202, 215)", textHeight: this.canvansH, lineHeight: this.canvansH - 10 }, //时间轴悬浮线
        underLine: { color: "rgb(194, 202, 215)", width: 1 }, //时间轴底线
        //回调事件
        mouseDown: {},//鼠标
        mouseUp: {},//鼠标弹起事件
        mouseMove: {},//鼠标移动事件
        mouseOut: {},//鼠标移出时间轴事件
        moveOnTimeLine: {}//时间轴移动结束事件 return index（选中的索引从0开始） time（移动后的时间戳）
    };
    this.timecell = options.timecell;
    $.extend(this._options, options);
    this.init(this.startTimestamp, this.timecell, false);
    return this;
};

/**
 * 初始化
 * @param {*} startTimestamp 最左侧时间
 * @param {*} timecell 录像段数组
 * @param {*} redrawFlag 是否重绘标记
 */
TimeSlider.prototype.init = function (startTimestamp, timecell, redrawFlag) {
    this.timecell = timecell;
    this.startTimestamp = startTimestamp;
    this.drawCellBg();
    this.addGraduations(startTimestamp); //绘制添加刻度
    if (typeof timecell != 'undefined')
        this.addCells(timecell); //添加时间组
    this.drawLine(0, this.canvansH, this.canvansW, this.canvansH, this._options.underLine.color, this._options.underLine
		.width); //底线
    // this.drawLine(this.canvansW / 2, 0, this.canvansW / 2, this._options.middleLine.height, this._options.middleLine.color,
    //	this._options.middleLine.width); //中间播放点时间线
    if (!redrawFlag) { //只有第一次进入才需要添加事件
        this.add_events();
    }
    if (this.timeLineBeginPos.length > 0) //画各个时间组的时间轴
    {
        // console.log("init timeLineArr",this.timeLineArr);
        // for (let i = 0; i < this.timeLineArr.length; i++) {
        //     this.timeLineArr[i].leftTopX = this.timeToCursorX(this.timeLineArr[i].beginTime);
        //     this.timeLineArr[i].rightButtonX = this.timeLineArr[i].leftTopX + this._options.middleLine.width;
        //     var timeLine = this.timeLineArr[i];
        //     this.drawLine(timeLine.leftTopX, timeLine.leftTopY, timeLine.leftTopX, timeLine.rightButtonY, this._options.middleLine.color,this._options.middleLine.width);
        // }
		console.log('this.timeLineBeginPos',this.timeLineBeginPos);
        for (let i = 0; i < this.timeLineBeginPos.length; i++) {
			var timeLine = this.timeLineBeginPos[i];
			this.drawLine(timeLine.leftTopX, timeLine.leftTopY, timeLine.leftTopX, timeLine.rightButtonY, this._options.middleLine.color,this._options.middleLine.width);
            this.ctx.fillStyle = "rgb(64, 196, 255)"; //提示框颜色
            this.ctx.fillText(this.changeTime(timeLine.beginTime), timeLine.leftTopX - 50, timeLine.leftTopY + this._options.cellHeight + 10);
        }
    }
   // var time = startTimestamp + (this.hours_per_ruler * 3600 * 1000) / 2;
    this.ctx.fillStyle = "rgb(64, 196, 255)"; //提示框颜色
    // this.ctx.fillText(this.changeTime(time), this.canvansW / 2 - 60, this.canvansH); //中间轴样式
}
/**
 * 自适应窗体事件
 */
TimeSlider.prototype.resize = function () {
    this.canvas.width = this.canvansW;
    this.init(this.startTimestamp, this.timecell, false);
}
/**
 * 判断点是否在正方形上
 */
TimeSlider.prototype.isCursorOnRectangle = function (inputX, inputY, leftTopX, leftTopY, rightButtonX, rightButtonY) {
    //console.log('inputX', inputX);
    //console.log('inputY', inputY);
    if ((inputX <= rightButtonX && inputX >= leftTopX) && (inputY >= leftTopY && inputY <= rightButtonY))
        return true;
    else
        return false;
}
/**
 * 绘制添加刻度
 * @param {*} startTimestamp 最左侧时间
 */
TimeSlider.prototype.addGraduations = function (startTimestamp) {
    var _this = this;
    var px_per_min = _this.canvansW / (_this.hours_per_ruler * 60); // px/min
    var px_per_ms = _this.canvansW / (_this.hours_per_ruler * 60 * 60 * 1000); // px/ms
    var px_per_step = _this.graduation_step; // px/格 默认最小值20px
    var min_per_step = px_per_step / px_per_min; // min/格
    for (var i = 0; i < _this.minutes_per_step.length; i++) {
        if (min_per_step <= _this.minutes_per_step[i]) { //让每格时间在minutes_per_step规定的范围内
            min_per_step = _this.minutes_per_step[i];
            px_per_step = px_per_min * min_per_step;
            break
        }
    }
    var medium_step = 30;
    for (var i = 0; i < _this.minutes_per_step.length; i++) {
        if (_this.distance_between_gtitle / px_per_min <= _this.minutes_per_step[i]) {
            medium_step = _this.minutes_per_step[i];
            break;
        }
    }
    var num_steps = _this.canvansW / px_per_step; //总格数
    var graduation_left;
    var graduation_time;
    var caretClass;
    var lineH;
    var ms_offset = _this.ms_to_next_step(startTimestamp, min_per_step * 60 * 1000); //开始的偏移时间 ms
    var px_offset = ms_offset * px_per_ms; //开始的偏移距离 px
    var ms_per_step = px_per_step / px_per_ms; // ms/step
    for (var i = 0; i < num_steps; i++) {
        graduation_left = px_offset + i * px_per_step; // 距离=开始的偏移距离+格数*px/格
        graduation_time = startTimestamp + ms_offset + i * ms_per_step; //时间=左侧开始时间+偏移时间+格数*ms/格
        var date = new Date(graduation_time);
        if (date.getUTCHours() == 0 && date.getUTCMinutes() == 0) {
            caretClass = 'big';
            lineH = this._options.lineH.big;
            var bigDate = _this.graduation_title(date);
            _this.ctx.fillText(bigDate, graduation_left - 20, 30);
            _this.ctx.fillStyle = this._options.dialtextColor;
        } else if (graduation_time / (60 * 1000) % medium_step == 0) {
            caretClass = 'middle';
            lineH = this._options.lineH.middle;
            var middle_date = _this.graduation_title(date);
            _this.ctx.fillText(middle_date, graduation_left - 20, 30);
            _this.ctx.fillStyle = this._options.dialtextColor;
        } else {
            lineH = this._options.lineH.def;
        }
        _this.drawLine(graduation_left, 0, graduation_left, lineH, this._options.dialColor, 1);
    }
}

/**
 * 绘制线
 * @param {*} beginX 
 * @param {*} beginY 
 * @param {*} endX 
 * @param {*} endY 
 * @param {*} color 
 * @param {*} width 
 */
TimeSlider.prototype.drawLine = function (beginX, beginY, endX, endY, color, width) {
    this.ctx.beginPath();
    this.ctx.moveTo(beginX, beginY);
    this.ctx.lineTo(endX, endY);
    this.ctx.strokeStyle = color;
    this.ctx.lineWidth = width;
    this.ctx.stroke();
}

/**
 * 添加录像段
 * @param {*} cells 录像数组
 */
TimeSlider.prototype.addCells = function (cells) {
    var _this = this;
    //	this.timeLineArr = [];
    cells.forEach(function (cell) {
        _this.drawCell(cell)
    });
}
/**
 * 设置录像数组时间轴的值
 * @param {*} data 录像数组时间轴的值
 *///{index:1,time:12000000}
TimeSlider.prototype.setTimeLine = function (data) {
    var _this = this;
    if (_this.timeLineBeginPos[data.index]) {
        // if (!_this.g_isMousedown || !_this.selectTimeLine.hasOwnProperty('index') || _this.selectTimeLine.index != data.index) {
        if (!_this.g_isMousedown || !_this.selectTimeLine.hasOwnProperty('index') || _this.selectTimeLine.chanel != data.chanel) {
            var posx = _this.timeToCursorX(data.time);
            _this.timeLineBeginPos[data.index].beginTime = data.time;
            _this.timeLineBeginPos[data.index].leftTopX = posx;
            _this.timeLineBeginPos[data.index].rightButtonX = posx + _this._options.middleLine.width;
        }
        _this.init(_this.startTimestamp, _this.timecell, true);
    }

    return _this.timeLineBeginPos[data.index].beginTime;
}
/**
 * 位置转换为时间戳
 * @param {*} currentX X轴位置
 */
TimeSlider.prototype.cursorXToTime = function (currentX) {
    var _this = this;
    var px_per_ms = _this.canvansW / (_this.hours_per_ruler * 60 * 60 * 1000); // px/ms
    var currentTime = currentX / px_per_ms + _this.startTimestamp;
    return currentTime;
}
/**
 * 时间戳转换为位置
 * @param {*} currentTime 时间戳
 */
TimeSlider.prototype.timeToCursorX = function (currentTime) {
    var _this = this;
    var px_per_ms = _this.canvansW / (_this.hours_per_ruler * 60 * 60 * 1000); // px/ms
    var currentX = (currentTime - _this.startTimestamp) * px_per_ms;
    return currentX;
}
/**
 * 绘制录像块
 * @param {*} cell cell包括beginTime ms;endTime ms;style 
 */
TimeSlider.prototype.drawCell = function (cell) {
    var _this = this;
    var px_per_ms = _this.canvansW / (_this.hours_per_ruler * 60 * 60 * 1000); // px/ms
    // var beginX = (cell.beginTime - _this.startTimestamp) * px_per_ms;
    var beginX = _this.timeToCursorX(cell.beginTime);
    var cell_width = (cell.endTime - cell.beginTime) * px_per_ms;
    var beginY = cell.beginY;
    _this.ctx.fillStyle = cell.style.background;
    _this.ctx.fillRect(beginX, beginY, cell_width, this._options.cellHeight);
    //绘制时间轴
    if (this.timeLineArr.length < _this.timecell.length) {
        this.timeLineArr.push({
            beginTime: cell.beginTime,
            //endTime:cell.endTime,
            leftTopX: beginX,
            leftTopY: beginY,
            rightButtonX: beginX + this._options.middleLine.width,
            rightButtonY: beginY + this._options.cellHeight,
            chanel: cell.chanel,
            simNum: cell.simNum
        }); //添加时间轴位置信息
        if (this.timeLineBeginPos.length > 0) {//添加时间轴开始位置信息
            var selectIndex = -1;
            var filterArr = this.timeLineBeginPos.filter(function (value, index, array) {
				console.log("filterArr");
                if (value.leftTopY == beginY) {
                    selectIndex = index;
                }
                return value.leftTopY == beginY;
            });
            if (filterArr.length > 0) {
                if (this.timeLineBeginPos[selectIndex].leftTopX > beginX) {
                    this.timeLineBeginPos[selectIndex].leftTopX = beginX;//获取最左边的开始位置
					this.timeLineBeginPos[selectIndex].beginTime = cell.beginTime;//获取最左边的开始位置
                }
            } else {
                this.timeLineBeginPos.push({
					beginTime:cell.beginTime,
					leftTopX: beginX,
					leftTopY: beginY,
					rightButtonX: beginX + this._options.middleLine.width,
					rightButtonY: beginY + this._options.cellHeight,
					chanel: cell.chanel,
					simNum: cell.simNum,
			})
            }
        }else{
			this.timeLineBeginPos.push({
					beginTime:cell.beginTime,
					leftTopX: beginX,
					leftTopY: beginY,
					rightButtonX: beginX + this._options.middleLine.width,
					rightButtonY: beginY + this._options.cellHeight,
					chanel: cell.chanel,
					simNum: cell.simNum,
			})
		}
    }
    //else if (this.timeLineArr.length == _this.timecell.length) {
    //    // console.log("timeLineArrtimeLineArr",this.timeLineArr);
    //    // this.drawLine(beginX, beginY, beginX, beginY + this.cellHeight, this._options.middleLine.color, 2);
    //}
}
/**
 * 绘制录像块背景
 */
TimeSlider.prototype.drawCellBg = function () {
    this.ctx.fillStyle = this._options.backgroundColor;
    this.ctx.fillRect(0, 0, this.canvansW, this.canvansH);
}
/**
 * 时间轴事件
 */
TimeSlider.prototype.add_events = function () {
    var _this = this;
    if (_this.canvas.addEventListener) {
        _this.canvas.addEventListener('DOMMouseScroll', _this.mousewheelFunc.bind(_this));
        _this.canvas.addEventListener('onwheel', _this.mousewheelFunc.bind(_this));
        _this.canvas.addEventListener('mousewheel', _this.mousewheelFunc.bind(_this));
        _this.canvas.addEventListener('mousedown', _this.mousedownFunc.bind(_this));
        _this.canvas.addEventListener('mousemove', _this.mousemoveFunc.bind(_this));
        _this.canvas.addEventListener('mouseup', _this.mouseupFunc.bind(_this));
        _this.canvas.addEventListener('mouseout', _this.mouseoutFunc.bind(_this));
    }
}
/**
 * 拖动/点击 mousedown事件
 */
TimeSlider.prototype.mousedownFunc = function (e) {
    this.g_isMousedown = true;
    this.g_mousedownCursor = this.get_cursor_x_position(e); //记住mousedown的位置
    if (typeof this._options.mouseDown == "function")
        this._options.mouseDown(this.g_mousedownCursor)
}

/**
 * 拖动/鼠标hover显示 mousemove事件
 */
TimeSlider.prototype.mousemoveFunc = function (e) {
    var _this = this;
    var posx = _this.get_cursor_x_position(e); //鼠标距离 px
    var posy = _this.get_cursor_y_position(e); //鼠标距离 px
    var px_per_ms = _this.canvansW / (_this.hours_per_ruler * 60 * 60 * 1000); // px/ms
    _this.clearCanvas();
    if (_this.g_isMousedown) {
        var diff_x = posx - _this.g_mousedownCursor;
        if (_this.canvas.style.cursor == "e-resize" && this.selectTimeLine) { //如果是拖动单个通道的时间轴则重新画时间轴
            if (this.timeLineBeginPos[this.selectTimeLine.index]) {
               // var ms_per_px = (_this.zoom * 3600 * 1000) / _this.canvansW; // ms/px             
                this.timeLineBeginPos[this.selectTimeLine.index].leftTopX = posx;
                this.timeLineBeginPos[this.selectTimeLine.index].rightButtonX = posx + 2;
                this.timeLineBeginPos[this.selectTimeLine.index].beginTime = _this.cursorXToTime(posx);
            }
        } else {

            _this.startTimestamp = _this.startTimestamp - Math.round(diff_x / px_per_ms);
            if (this.timeLineBeginPos.length > 0) { //更改时间轴相对位置
                for (let i = 0; i < this.timeLineBeginPos.length; i++) {
                    this.timeLineBeginPos[i].leftTopX = this.timeToCursorX(this.timeLineBeginPos[i].beginTime);
                    this.timeLineBeginPos[i].rightButtonX = this.timeLineBeginPos[i].leftTopX + this._options.middleLine.width;
                }
            }
        }
        _this.init(_this.startTimestamp, _this.timecell, true);
        _this.g_isMousemove = true;
        _this.g_mousedownCursor = posx;
    } else {
        var time = _this.startTimestamp + posx / px_per_ms;
        _this.hoverTime = time;
        _this.init(_this.startTimestamp, _this.timecell, true);
        _this.drawLine(posx, 0, posx, _this._options.hoverLine.lineHeight, _this._options.hoverLine.color, _this._options.hoverLine
			.width);
        _this.ctx.fillStyle = _this._options.hoverLine.textColor;
        _this.ctx.fillText(_this.changeTime(time), posx - 50, _this._options.hoverLine.textHeight);
        if (_this.timeLineBeginPos.length > 0) {
            for (let i = 0; i < _this.timeLineBeginPos.length; i++) {
                var sigleTimeLine = _this.timeLineBeginPos[i];
                var result = _this.isCursorOnRectangle(posx, posy, sigleTimeLine.leftTopX, sigleTimeLine.leftTopY, sigleTimeLine.rightButtonX,
					sigleTimeLine.rightButtonY);
                if (result) {
                    _this.canvas.style.cursor = "e-resize";
                    _this.selectTimeLine = {
                        index: i,
                        chanel: this.timeLineBeginPos[i].chanel,
                        simNum: this.timeLineBeginPos[i].simNum,
                    }
                    break;
                } else
                    _this.canvas.style.cursor = "pointer";
            }
        }
    }
    if (typeof this._options.mouseMove == "function")
        this._options.mouseMove()
}
/**
 * 拖动/点击 mouseup事件
 */
TimeSlider.prototype.mouseupFunc = function (e) {
    var _this = this;
    if (_this.g_isMousemove) { //拖动 事件
        _this.g_isMousemove = false;
        _this.g_isMousedown = false;
        if (this.selectTimeLine.hasOwnProperty('index')) {
            if (typeof this._options.moveOnTimeLine == "function")//添加
            {
                var _timeLine = this.timeLineBeginPos[this.selectTimeLine.index];
                this._options.moveOnTimeLine(this.selectTimeLine.index, _timeLine.beginTime, _timeLine.chanel, _timeLine.simNum);
            }
            this.selectTimeLine = {};
        }
        _this.returnTime = _this.startTimestamp + (_this.hours_per_ruler * 3600 * 1000) / 2;
    } else { // click 事件
        this.selectTimeLine = {};
        _this.g_isMousedown = false;
        var posx = _this.get_cursor_x_position(e); //鼠标距离 px
        var posy = _this.get_cursor_y_position(e); //鼠标距离 px
        var ms_per_px = (_this.zoom * 3600 * 1000) / _this.canvansW; // ms/px
        var returnTime = _this.startTimestamp + posx * ms_per_px;
    }
    if (typeof this._options.mouseUp == "function")
        this._options.mouseUp(posx, posy, this.returnTime)
}
/**
 * 鼠标移出隐藏时间 mouseout事件
 * @param {*} e 
 */
TimeSlider.prototype.mouseoutFunc = function () {
    var _this = this;
    _this.clearCanvas();
    _this.init(_this.startTimestamp, _this.timecell, true);
    if (typeof this._options.mouseOut == "function")
        this._options.mouseOut()
}
/**
 * 滚轮放大缩小，以时间轴中心为准 mousewheel事件
 */
TimeSlider.prototype.mousewheelFunc = function (event) {
    var _this = this;
    if (event && event.preventDefault) {
        event.preventDefault()
    } else {
        window.event.returnValue = false;
        return false;
    }
    var e = window.event || event;
    var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail)));
    // var middle_time = _this.startTimestamp + (_this.hours_per_ruler * 3600 * 1000) / 2; //ms 记住当前中间的时间
    var middle_time = _this.hoverTime;
    if (delta < 0) {
        _this.zoom = _this.zoom + 4;
        if (_this.zoom >= 24) {
            _this.zoom = 24; //放大最大24小时
        }
        _this.hours_per_ruler = _this.zoom;
    } else if (delta > 0) { // 放大
        _this.zoom = _this.zoom - 4;
        if (_this.zoom <= 1) {
            _this.zoom = 1; //缩小最小1小时
        }
        //console.log('_this.zoom', _this.zoom);
        _this.hours_per_ruler = _this.zoom;
    }
    _this.clearCanvas();

    _this.startTimestamp = middle_time - (_this.hours_per_ruler * 3600 * 1000) / 2; //startTimestamp = 当前中间的时间 - zoom/2
    if (_this.timeLineBeginPos.length > 0) { //更改时间轴相对位置
        for (let i = 0; i < _this.timeLineBeginPos.length; i++) {
            _this.timeLineBeginPos[i].leftTopX = _this.timeToCursorX(_this.timeLineBeginPos[i].beginTime);
            _this.timeLineBeginPos[i].rightButtonX = _this.timeLineBeginPos[i].leftTopX + this._options.middleLine.width;
        }
    }
    _this.init(_this.startTimestamp, _this.timecell, true)
}

/**
 * 获取鼠标posx
 * @param {*} e 
 */
TimeSlider.prototype.get_cursor_x_position = function (e) {
    var posx = 0;

    if (!e) {
        e = window.event;
    }
    if (e.pageX || e.pageY) {
        posx = e.pageX;
    } else if (e.clientX || e.clientY) {
        posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
    }
    return posx - this.canvas.offsetLeft; //相对于canvas横坐标的位置
}
/**
 * 获取鼠标posy
 * @param {*} e 
 */
TimeSlider.prototype.get_cursor_y_position = function (e) {
    var posy = 0;
    if (!e) {
        e = window.event;
    }
    if (e.pageX || e.pageY) {
        posy = e.pageY;
    } else if (e.clientX || e.clientY) {
        posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
    }
    return posy - $("#" + this.canvas.id).offset().top;
    //this.canvas.offsetTop; //相对于canvas横坐标的位置
}
/**
 * 返回时间轴上刻度的时间
 * @param {*} datetime new Date 格式
 */
TimeSlider.prototype.graduation_title = function (datetime) {
    if (datetime.getHours() == 0 && datetime.getMinutes() == 0 && datetime.getMilliseconds() == 0) {
        return ('0' + datetime.getDate().toString()).substr(-2) + '.' +
			('0' + (datetime.getMonth() + 1).toString()).substr(-2) + '.' +
			datetime.getFullYear();
    }
    return datetime.getHours() + ':' + ('0' + datetime.getMinutes().toString()).substr(-2);
};

/**
 * 返回 2018-01-01 10:00:00 格式时间
 * @param {*} time 
 */
TimeSlider.prototype.changeTime = function (time) {
    var newTime = new Date(time);
    var year = newTime.getFullYear();
    var month = newTime.getMonth() + 1;
    if (month < 10) {
        var month = "0" + month;
    }
    var date = newTime.getDate();
    if (date < 10) {
        var date = "0" + date;
    }
    var hour = newTime.getHours();
    if (hour < 10) {
        var hour = "0" + hour;
    }
    var minute = newTime.getMinutes();
    if (minute < 10) {
        var minute = "0" + minute;
    }
    var second = newTime.getSeconds();
    if (second < 10) {
        var second = "0" + second;
    }
    return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
}

/**
 * 左侧开始时间的偏移，返回单位ms
 * @param {*} timestamp 
 * @param {*} step 
 */
TimeSlider.prototype.ms_to_next_step = function (timestamp, step) {
    var remainder = timestamp % step;
    return remainder ? step - remainder : 0;
}

/**
 * 设置时间，让这个时间点跳到中间红线处
 *  @param {*} time 单位ms
 */
TimeSlider.prototype.setTimeToMiddle = function (time) {
    this.returnTime = time;
    this.clearCanvas();
    this.startTimestamp = time - (this.hours_per_ruler * 60 * 60 * 1000) / 2;
    this.init(this.startTimestamp, this.timecell, true)
}

/**
 * 返回点击或者拖动的时间点
 */
TimeSlider.prototype.returnMouseupTime = function (callback) {
    var _this = this;
    if (_this.returnTime != null) {
        if (callback) {
            callback(_this.returnTime);
        }
    }
}

/**
 * 清除canvas 每次重新绘制需要先清除
 */
TimeSlider.prototype.clearCanvas = function () {
    this.ctx.clearRect(0, 0, this.canvansW, this.canvansH);
}


TimeSlider.prototype.set_options = function (options) {
    $.extend(this._options, options);
    this.init(this.startTimestamp, this.timecell, false);
}


/**
 * 插件设置
 * @param {*} options 
 */
function Plugin(options, objData) { //time,cell,callback
    return this.each(function () {
        var _this = $(this);
        var _thisId = this.id;
        var data = _this.data('timeslider');
        if (!data) {
            _this.data('timeslider', new TimeSlider(_thisId, options)); //初始化
        } else {
            if (typeof options == 'string') {
                switch (options) {
                    case 'setTimeToMiddle':
                        data.setTimeToMiddle(objData.time);
                        break;
                    case 'returnMouseupTime':
                        data.returnMouseupTime(objData.callback);
                        break;
                    case 'init':
                        data.clearCanvas();
                        data.init(objData.time, objData.cell, true);
                        break;
                    case 'resize':
                        // data.clearCanvas();
                        data.resize();
                        break;
                    case 'setCell':
                        data.timecell = objData.cells;
                        //   data._options.timecell = objData.cells;
                        data.addCells(objData.cells);
                        break;
                    case 'setWidth':
                        data.canvansW = objData;
                        break;
                    case 'setHeight':
                        data.canvansH = objData;
                        break;
                    case 'reFresh':
                        data.init(data.startTimestamp, data.timecell, false);
                        break;
                    case 'setTimeLine'://{index:1,time:12000000}
                        data.init(data.startTimestamp, data.timecell, false);
                        break;
                }
            } else {
                data.set_options(options);
            }
        }
    });
}
var old = $.fn.TimeSlider;
$.fn.TimeSlider = Plugin;
$.fn.TimeSlider.noConflict = function () {
    $.fn.TimeSlider = old;
    return this;
};
